<?php

/**
 * registrotutoria actions.
 *
 * @package    ecampus
 * @subpackage registrotutoria
 * @author     Grupo 608
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class registrotutoriaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->registro_tutorias = Doctrine::getTable('RegistroTutoria')
      ->createQuery('a')
      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->id_usuario = $request->getParameter('id_usuario');
    $this->tutoria = Doctrine_Core::getTable('tutoria')->find($request->getParameter('id_tutoria'));
    $this->form = new RegistroTutoriaForm();
    $this->form->setTutoria($request->getParameter('id_tutoria'));
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new RegistroTutoriaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->id_usuario = $request->getParameter('id_usuario');

    $this->forward404Unless($registro_tutoria = Doctrine::getTable('RegistroTutoria')->find(array($request->getParameter('id_registro'))), sprintf('Object registro_tutoria does not exist (%s).', $request->getParameter('id_registro')));
    $this->form = new RegistroTutoriaForm($registro_tutoria);
    if(is_null($this->id_usuario))
    {
         $this->id_usuario = $registro_tutoria->getTutoria()->getDocente()->getIdUsuario();
         $this->redirect('@homepage');
    }

//    $docente = $registro_tutoria->getTutoria()->getDocente();
//    $this->redirect('docente/tutorias?id_usuario='.$docente->getIdUsuario() );
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($registro_tutoria = Doctrine::getTable('RegistroTutoria')->find(array($request->getParameter('id_registro'))), sprintf('Object registro_tutoria does not exist (%s).', $request->getParameter('id_registro')));
    $this->form = new RegistroTutoriaForm($registro_tutoria);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $this->id_usuario = $request->getParameter('id_usuario');
    $request->checkCSRFProtection();

    $this->forward404Unless($registro_tutoria = Doctrine::getTable('RegistroTutoria')->find(array($request->getParameter('id_registro'))), sprintf('Object registro_tutoria does not exist (%s).', $request->getParameter('id_registro')));
    $registro_tutoria->delete();

//    $this->redirect('registrotutoria/index');
    $docente = $registro_tutoria->getTutoria()->getDocente();
    $this->redirect('docente/tutorias?id_usuario='.$docente->getIdUsuario() );
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $registro_tutoria = $form->save();

      $docente = $registro_tutoria->getTutoria()->getDocente();
//      $this->redirect('registrotutoria/edit?id_registro='.$registro_tutoria->getIdRegistro());
      $this->redirect('docente/tutorias?id_usuario='.$docente->getIdUsuario() );
    }
  }
}
